<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\api\model\ProjectPerformanceClass as ProjectPerformanceClassModel;
use app\Request;

class PerformanceClass extends Base
{

    /**
     * 绩效考核分类列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function performanceClassList(Request $request)
    {
        try {

            $page = $request->param('page');
            $pagesize = $request->param('pagesize');
            $ProjectPerformanceClassModel = new ProjectPerformanceClassModel;
            $list = $ProjectPerformanceClassModel->order('sort asc')
                ->paginate([
                    'list_rows'=> $pagesize,
                    'var_page' => 'page',
                    'page'=>$page
                ]);
            if($list){
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$list];
            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 添加绩效考核分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function performanceAdd(Request $request){
        try {
            $data['name'] = $request->param('name');
            if($request->param('mark'))  $data['mark'] = $request->param('mark');
            if($request->param('sort'))  $data['sort'] = $request->param('sort');
            if($request->param('score')) $data['score'] = $request->param('score');
            if($request->param('weight')) $data['weight'] = $request->param('weight');
            $data['create_time'] = time();

            $ProjectPerformanceClassModel = new ProjectPerformanceClassModel();
            $performanceClass = $ProjectPerformanceClassModel->where('name','LIKE',$data['name'])->find();
            if($performanceClass){
                $result = ["status"=>false,"msg"=>"分类已存在！"];
                //return json($result);
            }

            $res = $ProjectPerformanceClassModel->insertGetId($data);
            if($res){
                $result = ["status"=>true,"msg"=>"添加成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"添加失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 编辑考核绩效分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function performanceSave(Request $request){
        try {

            $id = $request->param('id');
            if($request->param('name'))  $data['name'] = $request->param('name');
            if($request->param('mark'))  $data['mark'] = $request->param('mark');
            $data['sort'] = $request->param('sort')?$request->param('sort'):0;
            $data['score'] = $request->param('score')?$request->param('score'):0;
            $data['weight'] = $request->param('weight')?$request->param('weight'):0;
            $data['update_time'] = time();

            if(!$id){
                $result = ["status"=>false,"msg"=>"id必须！"];
                return json($result);
            }

            $ProjectPerformanceClassModel = new ProjectPerformanceClassModel();

            if(isset($data['name'])){
                $performanceClass = $ProjectPerformanceClassModel->where('name','LIKE',$data['name'])->find();
                if($performanceClass){
                    $result = ["status"=>false,"msg"=>"分类已存在！"];
                    //return json($result);
                }
            }

            $res = $ProjectPerformanceClassModel->where('id','LIKE',$id)->save($data);
            if($res !== false){
                $result = ["status"=>true,"msg"=>"编辑成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"编辑失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 删除考核绩效分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function delPerformanceClass(Request $request){
        try {

            $id = $request->param('id');
            $ProjectPerformanceClassModel = new ProjectPerformanceClassModel();
            $res = $ProjectPerformanceClassModel->where('id','=',$id)->delete();
            if($res){
                $result = ["status"=>true,"msg"=>"删除成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"删除失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
}
